package com.oracle.service.impl;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.oracle.compents.PageResult;
import com.oracle.mapper.PhUserMapper;
import com.oracle.pojo.PhUser;
import com.oracle.pojo.PhUserExample;
import com.oracle.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * 服务实现层
 * @author Administrator
 *
 */
@Service
public class UserServiceImpl implements UserService {

	@Autowired
	private PhUserMapper userMapper;

	/**
	 * 查询全部
	 */
	@Override
	public List<PhUser> findAll() {
		return userMapper.selectByExample(null);
	}

	/**
	 * 按分页查询
	 */
	@Override
	public PageResult findPage(int pageNum, int pageSize) {
		PageHelper.startPage(pageNum, pageSize);
		Page<PhUser> page=   (Page<PhUser>) userMapper.selectByExample(null);
		return new PageResult(page.getTotal(), page.getResult());
	}

	/**
	 * 增加
	 */
	@Override
	public void add(PhUser user) {
		userMapper.insert(user);
	}


	/**
	 * 修改
	 */
	@Override
	public void update(PhUser user){
		userMapper.updateByPrimaryKey(user);
	}

	/**
	 * 根据ID获取实体
	 * @param id
	 * @return
	 */
	@Override
	public PhUser findOne(Long id){
		return userMapper.selectByPrimaryKey(id);
	}

	/**
	 * 批量删除
	 */
	@Override
	public void delete(Long[] ids) {
		for(Long id:ids){
			userMapper.deleteByPrimaryKey(id);
		}
	}


		@Override
	public PageResult findPage(PhUser user, int pageNum, int pageSize) {
		PageHelper.startPage(pageNum, pageSize);

		PhUserExample example=new PhUserExample();
		PhUserExample.Criteria criteria = example.createCriteria();

		if(user!=null){
						if(user.getUsername()!=null && user.getUsername().length()>0){
				criteria.andUsernameLike("%"+user.getUsername()+"%");
			}			if(user.getPassword()!=null && user.getPassword().length()>0){
				criteria.andPasswordLike("%"+user.getPassword()+"%");
			}			if(user.getRealname()!=null && user.getRealname().length()>0){
				criteria.andRealnameLike("%"+user.getRealname()+"%");
			}			if(user.getBirthday()!=null && user.getBirthday().length()>0){
				criteria.andBirthdayLike("%"+user.getBirthday()+"%");
			}			if(user.getEmail()!=null && user.getEmail().length()>0){
				criteria.andEmailLike("%"+user.getEmail()+"%");
			}			if(user.getPhoto()!=null && user.getPhoto().length()>0){
				criteria.andPhotoLike("%"+user.getPhoto()+"%");
			}
		}

		Page<PhUser> page= (Page<PhUser>)userMapper.selectByExample(example);
		return new PageResult(page.getTotal(), page.getResult());
	}

}
